/*
 * Created on 2005-1-6
 *
 */
package com.taobao.common.dao.persistence;

import java.io.Serializable;

import com.taobao.common.dao.persistence.exception.DBRouterException;

/**
 * @author xingdian
 * @version $Id: DBRoute.java,v 1.4 2005/06/09 10:05:06 shenyu Exp $
 */
public class DBRoute implements Serializable {
  /** Comment for <code>serialVersionUID</code> */
  private static final long serialVersionUID = 3258135734605854519L;

  public static final int   BY_XID           = 0;

  public static final int   BY_USER          = 1;

  public static final int   BY_ITEM          = 2;

  private String            userId           = null;

  private String            itemId           = null;

  private String            xid              = null;

  private int               routingStrategy  = BY_XID;

  public static DBRoute getAssistantRoute() {
    DBRoute back = new DBRoute();

    back.setXid("assistant");

    return back;
  }

  public static DBRoute getDB1Route() {
    DBRoute back = new DBRoute();

    back.setXid("db1");

    return back;
  }

  public static DBRoute getOrderRoute() {
    DBRoute back = new DBRoute();

    back.setXid("order");

    return back;
  }

  public static DBRoute getOrderMiningRoute() {
    DBRoute back = new DBRoute();

    back.setXid("ordermining");

    return back;
  }

  public static DBRoute getDBRouteByUserId(String id) {
    DBRoute back = new DBRoute();

    back.setUserId(id);
    return back;
  }

  /**
   * ���userid��ȡDBRoute("db1" || "db2"), ���Ҹ��userid�ĵ�һ���ַ����ú�xid
   * ������SqlMapBaseDAOSupport.getDBRouteManager.getTemplates(dr, sqlid)����ȡ
   * 
   * @param userid
   * @return Ĭ��null
   * @author afei 2007-10-29
   */
  public static final DBRoute getDBRouteWithXidByUserid(String userid) {
    if (null == userid || 32 != userid.trim().length()) {
      return null;
    }
    userid = userid.trim();
    DBRoute dr = DBRoute.getDBRouteByUserId(userid);
    String db1 = "01234567";
    String db2 = "89abcdef";
    if (0 == userid.indexOf(db1)) {
      dr.setXid("db1");
    } else if (0 == userid.indexOf(db2)) {
      dr.setXid("db2");
    } else {
      return null;
    }
    return dr;
  }

  public DBRoute() {
    super();
  }

  public DBRoute(String xid) {
    this.xid = xid;
  }

  /**
   * @deprecated
   * @return Returns the itemId.
   */
  public String getItemId() {
    return itemId;
  }

  /**
   * @param itemId
   * @deprecated The itemId to set.
   */
  public void setItemId(String itemId) {
    this.itemId = itemId;
    routingStrategy = BY_ITEM;
  }

  /**
   * @return Returns the routingStrategy.
   */
  public int getRoutingStrategy() {
    return routingStrategy;
  }

  /**
   * @param routingStrategy
   *          The routingStrategy to set.
   */
  public void setRoutingStrategy(int routingStrategy) {
    this.routingStrategy = routingStrategy;
  }

  /**
   * @return Returns the userId.
   */
  public String getUserId() {
    return userId;
  }

  /**
   * @param userId
   *          The userId to set.
   */
  public void setUserId(String userId) {
    this.userId = userId;
    routingStrategy = BY_USER;
  }

  /**
   * @return Returns the xid.
   */
  public String getXid() {
    return xid;
  }

  /**
   * @param xid
   *          The xid to set.
   */
  public void setXid(String xid) {
    this.xid = xid;
    routingStrategy = BY_XID;
  }

  public String toString() {
    String str = "";

    if ((routingStrategy == BY_XID) && (xid != null)) {
      str += (routingStrategy + xid);

      return encode(str);
    }

    if ((routingStrategy == BY_USER) && (userId != null)) {
      str += (routingStrategy + userId);

      return encode(str);
    }

    if ((routingStrategy == BY_ITEM) && (itemId != null)) {
      str += (routingStrategy + itemId);

      return encode(str);
    }

    return null;
  }

  public static DBRoute parse(String encodedStr) throws DBRouterException {
    DBRoute pq = new DBRoute();

    if (encodedStr == null) {
      return pq;
    }

    String decodedStr = decode(encodedStr);
    char rs = decodedStr.charAt(0);
    String value = decodedStr.substring(1);

    if (((rs - '0') == BY_XID) && (value != null)) {
      pq.setXid(value);

      return pq;
    }

    if (((rs - '0') == BY_USER) && (value != null)) {
      pq.setUserId(value);

      return pq;
    }

    if (((rs - '0') == BY_ITEM) && (value != null)) {
      pq.setItemId(value);

      return pq;
    }

    return pq;
  }

  private static String encode(String str) {
    return str;
  }

  private static String decode(String str) throws DBRouterException {
    if (validate(str) != true) {
      throw new DBRouterException("Invalidated route string");
    }

    return str;
  }

  private static boolean validate(String str) {
    return true;
  }

  public boolean isEmpty() {
    return ((xid == null) && (userId == null) && (itemId == null));
  }
}